package com.ordprovider.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.cusprovider.dao.AuthorityDao;
import com.cusprovider.dao.RoleDao;
import com.cusprovider.dao.StaffDao;
import com.pojo.Role;
import com.service.RoleService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Component
@Transactional
@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleDao roleDao ;

    @Resource
    private StaffDao staffDao ;

    @Resource
    private AuthorityDao authorityDao ;

    @Override
    public List<Role> getAllRoles() {
        return roleDao.getAllRoles();
    }

    @Override
    public Role getRoleInfoById(int rid) {
        return roleDao.getRoleInfoById(rid);
    }

    @Override
    public int addRole(Role role) {
        return roleDao.addRole(role);
    }

    @Override
    public int updateRole(Role role) {
        return roleDao.updateRole(role);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int deleteRole(int rid) {
        int success = 0 ;

        //先删除角色下对应的用户角色中间表
        staffDao.deleteStaffRoleByRole(rid) ;


        //再删除角色下对应的权限中间表
        authorityDao.deleteAuthorityByRole(rid) ;


        //最后删除角色
        roleDao.deleteRole(rid) ;


        success = 1 ;

        return success;
    }
}
